package org.fireflow2.designer.eclipse.figures;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PositionConstants;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.Image;
import org.fireflow.pdl.fpdl20.diagram.basic.Circle;
import org.fireflow.pdl.fpdl20.process.EndNode;
import org.fireflow.pdl.fpdl20.process.features.Feature;
import org.fireflow.pdl.fpdl20.process.features.endnode.NormalEndFeature;
import org.fireflow.pdl.fpdl20.process.features.endnode.ThrowCompensationFeature;
import org.fireflow.pdl.fpdl20.process.features.endnode.ThrowFaultFeature;
import org.fireflow.pdl.fpdl20.process.features.endnode.ThrowTerminationFeature;
import org.fireflow2.designer.eclipse.Activator;
import org.fireflow2.designer.eclipse.modelwrapper.basic.EndNodeWrapper;
import org.fireflow2.designer.eclipse.util.Util4Eclipse;

public class EndNodeFigure extends Label implements CommonFigureInterface {
	EndNodeWrapper endNodeWrapper  = null;
	public static final int IconTextGap = 5;
	public static final int MaxTextHeight = 20;
	public static final int MaxTextWidth = 50;
	
	static Map<String,Image> imageFileMap = new HashMap<String,Image>();
	{
		ImageDescriptor imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/end_none_event.png");
		imageFileMap.put(NormalEndFeature.class.getName(), imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/end_compensation_event.png");
		imageFileMap.put(ThrowCompensationFeature.class.getName(), imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/end_error_event.png");
		imageFileMap.put(ThrowFaultFeature.class.getName(), imgDesc.createImage());
		
		imgDesc = ImageDescriptor.createFromFile(Activator.class, "resources/obj32/end_terminate_event.png");
		imageFileMap.put(ThrowTerminationFeature.class.getName(),  imgDesc.createImage());
		
	}
	
	
	protected boolean useLocalCoordinates() {
		return true;
	}
	
	public org.eclipse.draw2d.geometry.Rectangle getBox(){
		org.eclipse.draw2d.geometry.Rectangle rectangle = new org.eclipse.draw2d.geometry.Rectangle(this.getIconBounds());
		return rectangle;
	}
	
	
	public Rectangle getSelectionHandleBounds(){
		Rectangle rectangle = new Rectangle(this.getIconBounds());
		this.translateToAbsolute(rectangle);
		rectangle.translate(CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_LOCATION_TRANSLATE_VALUE);
		rectangle.resize(CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE,
				CommonFigureInterface.BOUNDS_SIZE_TRANSLATE_VALUE);
		return rectangle;
	}
	
	public String getDisplayName(){
		Circle theCircleShape = (Circle)endNodeWrapper.getDiagramElement().getShape();
		String displayName = endNodeWrapper.getDisplayName();

		if (!org.fireflow.pdl.fpdl20.diagram.basic.Label.CONTENT_FROM_WORKFLOW_ELEMENT.equals(theCircleShape.getTitle())){
			displayName = theCircleShape.getTitle();
		}
		return displayName;
	}
	
	public EndNodeFigure(EndNodeWrapper wrapper){
		endNodeWrapper = wrapper ;

		Circle theCircleShape = (Circle)endNodeWrapper.getDiagramElement().getShape();
		
		this.setOpaque(false);
		
		this.setText(getDisplayName());
		this.setFont(Util4Eclipse.getFont(theCircleShape.getLabel()));
		this.setTextPlacement(PositionConstants.SOUTH);
		this.setTextAlignment(CENTER);
		this.setIconAlignment(CENTER);
		this.setIconTextGap(IconTextGap);
		this.setIcon(imageFileMap.get(NormalEndFeature.class.getName()));
	}
	public void refreshFigure(){
		Circle theCircleShape = (Circle)endNodeWrapper.getDiagramElement().getShape();
		EndNode endNode = (EndNode)endNodeWrapper.getWorkflowModelElement();

		this.setText(getDisplayName());
		this.setFont(Util4Eclipse.getFont(theCircleShape.getLabel()));
		

		if (endNode!=null){
			Feature f = endNode.getFeature();
			Image image = imageFileMap.get(NormalEndFeature.class.getName());
			if (f==null || f instanceof NormalEndFeature){
				image = imageFileMap.get(NormalEndFeature.class.getName());
			}else if (f instanceof ThrowCompensationFeature){
				image = imageFileMap.get(ThrowCompensationFeature.class.getName());
			}
			else if (f instanceof ThrowFaultFeature){
				image = imageFileMap.get(ThrowFaultFeature.class.getName());
			}
			else if (f instanceof ThrowTerminationFeature){
				image = imageFileMap.get(ThrowTerminationFeature.class.getName());
			}
			
			if (image!=null){
				this.setIcon(image);
			}
		}
		
		repaint();
	}

}
